package com.semac.client.paginas.master;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.semac.client.aplicacao.AplicacaoController;
import com.semac.client.aplicacao.PaginasController;
import com.semac.client.componentes.botao.ButtonClickEvent;
import com.semac.client.componentes.botao.ButtonWidget;
import com.semac.client.paginas.chamada.ChamadaUI;
import com.semac.client.paginas.inscricao.InscricaoUI;
import com.semac.client.paginas.inscricaoatividade.InscricaoAtividadeUI;
import com.semac.client.paginas.perfil.PerfilUI;
import com.semac.client.rpc.ServicoAutenticacaoRPC;
import com.semac.client.rpc.ServicoAutenticacaoRPCAsync;

public class MasterPageUI extends Composite {

	private static MasterPageUIUiBinder uiBinder = GWT.create(MasterPageUIUiBinder.class);
	@UiField VerticalPanel contentPanel;
	@UiField ButtonWidget meuPerfilBtn;
	@UiField ButtonWidget ministranteBtn;
	@UiField ButtonWidget inscSemacBtn;
	@UiField ButtonWidget comexBtn;
	@UiField ButtonWidget comgradBtn;
	@UiField Label buttonLogout;

	private ServicoAutenticacaoRPCAsync servicoAutenticacao = ServicoAutenticacaoRPC.Util.getInstance();
	
	
	interface MasterPageUIUiBinder extends UiBinder<Widget, MasterPageUI> {
	}

	public MasterPageUI() {
		initWidget(uiBinder.createAndBindUi(this));
		this.refreshButonsState();		
	}

	/**
	 * seta visibilidade dos botoes
	 */
	private void refreshButonsState() {
		boolean taLogado = AplicacaoController.getPessoaLogada() != null;
		boolean ehMinistrante = false;
		boolean ehComex = false;
		boolean ehComgrad = false;
		
		if (AplicacaoController.getPessoaLogada() != null ){
			ehMinistrante = AplicacaoController.getPessoaLogada().ehMinistrante();
			ehComex = AplicacaoController.getPessoaLogada().ehComex();
			ehComgrad = AplicacaoController.getPessoaLogada().ehComgrad();
		}
		
		buttonLogout.setVisible(taLogado);
		meuPerfilBtn.setVisible(taLogado);
		inscSemacBtn.setVisible(taLogado && !ehComex && !ehComgrad); 
		ministranteBtn.setVisible(taLogado && ehMinistrante);
		comexBtn.setVisible(taLogado && ehComex);
		comgradBtn.setVisible(taLogado && ehComgrad);
	}
	
	
	public void setMainContent(Widget view) {
		if (view == null)
			return;
		contentPanel.clear();
		contentPanel.add(view);
		this.refreshButonsState();
	}
	
	//abaixo os handlers dos botoes. Cada botao clicado chama uma nova Pagina
	@UiHandler("ministranteBtn")
	void onMinistranteBtnClick(ButtonClickEvent event) {
		PaginasController.getInstance().navegaParaPagina(new ChamadaUI());
	}
	
	@UiHandler("inscSemacBtn")
	void onInscSemacBtnClick(ButtonClickEvent event) {
		PaginasController.getInstance().navegaParaPagina(new InscricaoUI());
	}
	
	@UiHandler("comexBtn")
	void onComexBtnClick(ButtonClickEvent event) {
		PaginasController.getInstance().navegaParaPagina(new InscricaoAtividadeUI());
	}
	
	@UiHandler("comgradBtn")
	void onComgradBtnClick(ButtonClickEvent event) {
	}
	
	@UiHandler("meuPerfilBtn")
	void onMeuPerfilBtnClick(ButtonClickEvent event) {
		PaginasController.getInstance().navegaParaPagina(new PerfilUI());
	}
	
	@UiHandler("buttonLogout")
	void onButtonLogoutClick(ClickEvent event) {
		servicoAutenticacao.fazLogout(new AsyncCallback<Void>() {			
			@Override
			public void onSuccess(Void result) {
				PaginasController.getInstance().removeHistoryToken();
				AplicacaoController.getInstance().refresh();			
			}
			
			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Falha na comunicação com servidor.");
			}
		});
	}
}
